package cn.elwy.rcp.ui.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;

import cn.elwy.common.util.AssertUtil;

/**
 * 文件选择对话框工具类
 * @author huangsq
 * @version 1.0, 2014-08-29
 * @since 1.0, 2014-08-29
 */
public class PathSelectedUtil {

  /**
   * 获取选择的文件列表
   * @param shell
   * @param filterPath 初始路径
   * @param filterNames 过滤名
   * @param filterExtensions 过滤扩展名
   * @return
   */
  public static List<String> getMultiSelectFile(Shell shell, String filterPath, String[] filterNames,
      String[] filterExtensions) {
    return getMultiSelectFile(shell, "Select", filterPath, filterNames, filterExtensions);
  }

  /**
   * 获取选择的文件列表
   * @param shell
   * @param title 窗口标题
   * @param filterPath 初始路径
   * @param filterNames 过滤名
   * @param filterExtensions 过滤扩展名
   * @return
   */
  public static List<String> getMultiSelectFile(Shell shell, String title, String filterPath, String[] filterNames,
      String[] filterExtensions) {
    FileDialog dialog = getFileDialog(shell, title, filterPath, filterNames, filterExtensions, SWT.MULTI);
    String fn = dialog.open();
    List<String> filePathList = new ArrayList<String>();
    if (fn != null) {
      String[] files = dialog.getFileNames();
      String filePath = dialog.getFilterPath();
      for (String fileName : files) {
        filePathList.add(filePath + File.separator + fileName);
      }
    }
    return filePathList;
  }

  /**
   * 获取选择的单个文件
   * @param shell
   * @param filterPath 初始路径
   * @param filterNames 过滤名
   * @param filterExtensions 过滤扩展名
   * @return
   */
  public static String getSelectFile(Shell shell, String filterPath, String[] filterNames, String[] filterExtensions) {
    return getSelectFile(shell, "Select", filterPath, filterNames, filterExtensions);
  }

  /**
   * 获取选择的单个文件
   * @param shell
   * @param title 窗口标题
   * @param filterPath 初始路径
   * @param filterNames 过滤名
   * @param filterExtensions 过滤扩展名
   * @return
   */
  public static String getSelectFile(Shell shell, String title, String filterPath, String[] filterNames,
      String[] filterExtensions) {
    FileDialog dialog = getFileDialog(shell, title, filterPath, filterNames, filterExtensions, SWT.OPEN);
    return dialog.open();
  }

  /**
   * 获取选择的目录
   * @param shell
   * @param title 窗口标题
   * @param message 窗口提示信息
   * @param filterPath 初始路径
   * @return
   */
  public static String getSelectDirectory(Shell shell, String title, String message, String filterPath) {
    DirectoryDialog dialog = new DirectoryDialog(shell);
    dialog.setText(title);
    dialog.setMessage(message);
    dialog.setFilterPath(filterPath);
    return dialog.open();
  }

  /**
   * 获取保存的文件路径
   * @param shell
   * @param title 窗口标题
   * @param filterPath 初始路径
   * @param filterNames 过滤名
   * @param filterExtensions 过滤扩展名
   * @return
   */
  public static String getSavePath(Shell shell, String filterPath, String[] filterNames, String[] filterExtensions) {
    return getSavePath(shell, "Save", filterPath, filterNames, filterExtensions);
  }

  /**
   * 保存文件到指定位置
   * @param shell
   * @param title 窗口标题
   * @param filterPath 初始路径
   * @param filterNames 过滤名
   * @param filterExtensions 过滤扩展名
   * @return
   */

  public static String getSavePath(Shell shell, String title, String filterPath, String[] filterNames,
      String[] filterExtensions) {
    FileDialog dialog = getFileDialog(shell, title, filterPath, filterNames, filterExtensions, SWT.SAVE);
    return dialog.open();
  }

  /**
   * 获取文件对话框窗口
   * @param shell
   * @param title 窗口标题
   * @param filterPath 初始路径
   * @param filterNames 过滤名
   * @param filterExtensions 过滤扩展名
   * @param style 样式
   * @return
   */
  private static FileDialog getFileDialog(Shell shell, String title, String filterPath, String[] filterNames,
      String[] filterExtensions, int style) {
    FileDialog dialog = new FileDialog(shell, style);
    if (AssertUtil.isNotEmpty(title)) {
      dialog.setText(title);
    }
    if (AssertUtil.isNotEmpty(filterPath)) {
      dialog.setFilterPath(filterPath);
    }
    if (AssertUtil.isNotEmpty(filterNames)) {
      dialog.setFilterNames(filterNames);
    }
    if (AssertUtil.isNotEmpty(filterExtensions)) {
      dialog.setFilterExtensions(filterExtensions);
    }
    return dialog;
  }

  // public getOpen() {
  // SelectionAdapter s = new SelectionAdapter() {
  // public void widgetSelected(SelectionEvent event) {
  // // User has selected to open multiple files
  // Shell activeShell = event.widget.getDisplay().getActiveShell();
  // FileDialog dlg = new FileDialog(activeShell, SWT.MULTI);
  // dlg.setFilterNames(FILTER_NAMES);
  // dlg.setFilterExtensions(FILTER_EXTS);
  // String fn = dlg.open();
  // if (fn != null) {
  // // Append all the selected files. Since getFileNames() returns only
  // // the names, and not the path, prepend the path, normalizing
  // // if necessary
  // StringBuffer buf = new StringBuffer();
  // String[] files = dlg.getFileNames();
  // for (int i = 0, n = files.length; i < n; i++) {
  // buf.append(dlg.getFilterPath());
  // if (buf.charAt(buf.length() - 1) != File.separatorChar) {
  // buf.append(File.separatorChar);
  // }
  // buf.append(files[i]);
  // buf.append(" ");
  // }
  // fileName.setText(buf.toString());
  // }
  // }
  // };
  // }
  // public void widgetSelected(SelectionEvent event) {
  // DirectoryDialog1 fd = new DirectoryDialog1(s, SWT.SELECTED);
  // //FileDialog fd = new FileDialog(s, SWT.SELECTED);
  // fd.setText("Open");
  // fd.setFilterPath("C:/");
  // String[] filterExt = { "*.txt", "*.doc", ".rtf", "*.*" };
  // //fd.setFilterExtensions(filterExt);
  // String selected = fd.open();
  // System.out.println(selected);
  // }
  //
  // public void widgetDefaultSelected(SelectionEvent event) {
  // }

}
